package whu.o2o.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.protocol.HTTP;




import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class Utils {
	public static String socketIP = "192.168.1.109";
	public static int POST = 7200; // 7198 4322
	public static String apkName = "旅游面对面";
	public static long audioTime;
	public static double latitude = 0;
	public static double longitude = 0;
	public static String recordStopTag = "com.cyht.dcjr.recordstop";
	public static String audioStartTag = "com.cyht.dcjr.AudioStart";
	public static String audioStopTag = "com.cyht.dcjr.AudioStop";
	public static String audioBackTag = "wh.che.audioBack";
	public static String sendTag = "com.cyht.dcrj.sendTag";
	public static String sendError = "com.cyht.dcjr.sendError"; // 发送信息失败
	public static String sendSucess = "com.cyht.dcjr.sendSucess"; // 信息发送成功
	public static String messagetalkTag = "com.cyht.dcjr.messagetalk";
	public static String messagelogTag = "com.cyht.dcjr.messagelog";
	public static LinkedList<String> keyList = new LinkedList<String>();
	public static boolean mTag = false;
	public static final String AUDIO_PATH = Environment.getExternalStorageDirectory() + "/f2f.guest/audio/";

	/**
	 * 将EditText的光标定位到字符的最后面
	 * 
	 * @param editText
	 */
	public static void setEditTextCursorLocation(EditText editText) {
		CharSequence text = editText.getText();
		if (text instanceof Spannable) {
			Spannable spanText = (Spannable) text;
			Selection.setSelection(spanText, text.length());
		}
	}

	/**
	 * 显示输入法
	 * 
	 * @param context
	 * @param view
	 */
	public static void showIme(Context context, EditText view) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
	}

	/**
	 * 隐藏输入法
	 */
	public static void hideIme(Activity context) {
		View view = context.getWindow().peekDecorView();
		if (view != null && view.getWindowToken() != null) {
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (imm.isActive()) {
				imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
			}
		}
	}

	/**
	 * 过滤城市名
	 * 
	 * @param city
	 * @return
	 */
	public static String filterCityName(String city) {
		if (!TextUtils.isEmpty(city) && city.length() > 2) {
			char c = city.charAt(city.length() - 1);
			if ('市' == c || '县' == c) {
				city = city.substring(0, city.length() - 1);
			}
		}
		return city;
	}

	/**
	 * 判断字符串是否全由数字组成
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * String转换为int
	 * 
	 * @param str
	 * @return
	 */
	public static int mystr2int(String str) {
		if (TextUtils.isEmpty(str) || !Utils.isNumeric(str)) {
			return 456;
		}

		return Integer.parseInt(str);
	}
	
	/**
	 * String转换为int
	 * 
	 * @param str
	 * @return
	 */
	public static int str2int(String str) {
		if (TextUtils.isEmpty(str) || !Utils.isNumeric(str)) {
			return 0;
		}

		return Integer.parseInt(str);
	}

	/**
	 * 将dip或dp值转换为px值，保证尺寸大小不变
	 * 
	 * @param dipValue
	 * @param scale
	 *            （DisplayMetrics类中属性density）
	 * @return
	 */
	public static int dip2px(Context context, float dipValue) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 * 
	 * @param spValue
	 * @param fontScale
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static int sp2px(Context context, float spValue) {
		float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}

	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^1[1-9][0-9]\\d{4,8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	/**
	 * 将bitmap转换成bytes
	 * 
	 * @param bitmap
	 * @return
	 */
	public static byte[] bitmapToBytes(Bitmap bitmap, String path) {
		int size = bitmap.getWidth() * bitmap.getHeight() * 4;
		ByteArrayOutputStream out = new ByteArrayOutputStream(size);
		try {
			if (path != null && !"".equals(path)) {
				String _path = path.toLowerCase();
				if ("png".equals(_path)) {
					bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
				} else if ("jpg".equals(_path) || "jpeg".equals(_path)) {
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
				}
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			return null;
		}
		return out.toByteArray();
	}

	public static String encodeString(String text) {
		try {
			return URLEncoder.encode(text, HTTP.UTF_8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}
}
